# cython: language_level=3
import os
import aioredis

class RedisClient:
    def __init__(self, redis_url):
        self.redis_url = redis_url
        self.redis_pool = None

    async def connect(self):
        if self.redis_pool is None:
            self.redis_pool = await aioredis.from_url(
                os.getenv("REDIS_URL", "redis://127.0.0.1:6379"),
                password=os.getenv("REDIS_PASSWD", "111111"),
                encoding="utf-8",
                decode_responses=True
            )

    async def disconnect(self):
        if self.redis_pool is not None:
            self.redis_pool.close()
            # await self.redis_pool.wait_closed()
            await self.redis_pool.close()
            self.redis_pool = None

    async def get(self, key):
        return await self.redis_pool.get(key)

    async def set(self, key, value):
        await self.redis_pool.set(key, value)

    async def incrby(self, key, increment):
        return await self.redis_pool.incrby(key, increment)